package com.ttmall.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ttmall.common.pojo.EasyUITreeNode;
import com.ttmall.mapper.TbItemCatMapper;
import com.ttmall.pojo.TbItemCat;
import com.ttmall.pojo.TbItemCatExample;
import com.ttmall.pojo.TbItemExample;
import com.ttmall.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by tianyi on 2018/1/13.
 */
@Service
public class ItemCatServiceImpl implements ItemCatService{

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public List<EasyUITreeNode> getItemCatList(long parentId) {

        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(example);

        List<EasyUITreeNode> result = new ArrayList<>();
        for (TbItemCat tbItemCat : tbItemCats) {
            EasyUITreeNode node = new EasyUITreeNode();
            node.setId(tbItemCat.getId());
            node.setText(tbItemCat.getName());
            node.setState(tbItemCat.getIsParent()? "closed" : "open");
            result.add(node);
        }
        return result;
    }
}
